package com.threeti.mecool.core.infrastructure.persistence;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.threeti.mecool.core.domain.model.Memo;

public abstract class MemoCriteria {
  
  public static Specification<Memo> eqUserId(final Long userId) {
    return new Specification<Memo>() {
      @Override
      public Predicate toPredicate(Root<Memo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Path<String> userIdPath = root.get("user").get("id");
        return builder.equal(userIdPath, userId);
      }
    };
  }
  
  public static Specification<Memo> likeChannelName(final String channelName) {
    return new Specification<Memo>() {
      @Override
      public Predicate toPredicate(Root<Memo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Path<String> channelNamePath = root.get("channel").get("name");
        return builder.like(channelNamePath, "%"+channelName+"%");
      }
    };
  }
}
